package controller;

import dao.UsersDao;
import model.Users;
import util.SHA;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterController extends HttpServlet {

    /**
     * 重定向至注册页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.getRequestDispatcher("/register.jsp").forward(req,resp);
    }

    /**
     * 用户注册
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //用户已经登录
        if(req.getSession().getAttribute("users") != null){
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
            return;
        }
        //		防止表单中非字符输入乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String name = (String) req.getParameter("name");
        String password = (String) req.getParameter("password");
        String rpassword = (String) req.getParameter("rpassword");
        String email = (String)req.getParameter("email");
//		用户名和密码验证
        System.out.println(name);
        if (name == null || name.length() ==0){
            req.setAttribute("error","用户名不能为空");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        if(name.length() > 10){
            req.setAttribute("error","用户名长度不能大于10");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        if(password == null ||password.length()==0){
            req.setAttribute("error","密码不能为空");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }

        if(rpassword == null ||rpassword.length()==0){
            req.setAttribute("error","重复密码不能为空");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        if (email == null || email.length()==0){
            req.setAttribute("error","邮箱不能为空");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        if(!password.equals(rpassword)){
            req.setAttribute("error","两次密码不一致");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        Users users = new Users();
        users.setName(name);
        users.setEmail(email);
        //通过SHA加密后写入数据库
        users.setPassword(SHA.encrypt(password));

        UsersDao usersDao = new UsersDao();
        String idusers =usersDao.addUsers(users);
        if (idusers.equals("exist")){
            req.setAttribute("error","用户名已存在");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
            return;
        }
        users.setPassword("");
        req.getSession().setAttribute("users",users);
        req.getSession().setMaxInactiveInterval(14400);//用户信息写入session session有效时间是4小时
        resp.sendRedirect(req.getContextPath()+"/login.jsp");
    }
}
